import tornado.web
import tornado.httpclient
import json

from bot.enterprise_wechat_gpt.util.transfer_util import *
from bot.enterprise_wechat_gpt.config.conf import *
from bot.enterprise_wechat_gpt.util.conversation_history_util import ConversationHistory

tair = TairUtil().tair
conversationHistory = ConversationHistory()


class TransferHandler(tornado.web.RequestHandler):

    def get(self):
        user_id = self.get_argument('userId', '')
        transfer = self.get_argument('transfer', 'false')
        output = self.run(user_id, transfer)
        self.write(json.dumps(output))

    def run(self, user_id, transfer):
        logging.info(f"user_id:[{user_id}] 设置转人:{transfer}")
        last_transfer = get_transfer(user_id)
        if last_transfer is True and transfer == 'false':
            logging.warning(f"user_id:[{user_id}] 从坐席接管, 转为AI自管 transfer=false")
            mark_from_agent_to_ai(user_id)
            # conversationHistory.add_system_msg(user_id, "转为AI自管", transfer=False)
        elif last_transfer is False and transfer == 'true':
            logging.warning(f"user_id:[{user_id}] 从AI自管, 转为坐席接管 transfer=true")
            # conversationHistory.add_system_msg(user_id, "转为AI转坐席", transfer=True)
            # mark_from_ai_to_agent(user_id)
        set_transfer(user_id, transfer)
        return {'status': 200}
